package com.ys.controller.api.storage;

import com.ys.config.MenuId;
import com.ys.entity.storage.TccFbaFeeDetails;
import com.ys.entity.storage.TccFbaFeeDetails;
import com.ys.entity.storage.TccFbaFeeDetails;
import com.ys.entity.storage.TccFbaPlan;
import com.ys.service.storage.TccFbaFeeDetailsService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: xjx
 * @CreateTime: 2024-12-05
 * @Description: 预估费用申请
 * @Version: 1.5
 */
@Controller
@RequestMapping("/api/inter/tccFbaFeeDetails")
public class ApiInterTccFbaFeeDetailsController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccFbaFeeDetailsController.class);

    @Autowired
    private TccFbaFeeDetailsService tccFbaFeeDetailsService;

    @Autowired
    private CommonApiService<TccFbaFeeDetails> commonApiService;

    //菜单id
    private Long menuId = MenuId.TCC_FbaFeeDetails;

//    @RequiresPermissions(PERMISSIONS_VIEW)
    @RequestMapping("/searchsearchEstimated")
    @ResponseBody
    public LayuiTableVoJA searchEstimated(@ModelAttribute TccFbaFeeDetails cond, String conditions){
        cond = setSeachSql(cond)
                .where("A.SourceType=0");

        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }

//    @RequiresPermissions(PERMISSIONS_VIEW)
    @RequestMapping("/searchActual")
    @ResponseBody
    public LayuiTableVoJA searchActual(@ModelAttribute TccFbaFeeDetails cond, String conditions){
        cond = setSeachSql1(cond)
//                .addLeftJoin("TCC_FbaPlan tfp2 ON tfp2.Bid=A.SourceBid")
//                .addLeftJoin("YS_ProcInst P2 ON P2.Bid = tfp2.Bid ")
//                .addField("tfp2.Bid bid,tfp2.CreationMethod creationMethod,tfp2.DeliverState deliverState,tfp2.Vgsbm vgsbm,tfp2.VgsJc vgsJc,tfp2.ShopName shopName,tfp2.AuditingTime auditingTime,tfp2.AuditingUser auditingUser")
                .where("A.SourceType=1");

        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }



    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TccFbaFeeDetails setSeachSql(TccFbaFeeDetails cond){

        cond.addLeftJoin("TCC_FbaBoxUp D ON D.BidFbaPlan = A.SourceBid AND A.SourceType = 0")
                .addField("D.bid fbaBoxUpBid")
                .addLeftJoin("TCC_FbaPlan B ON B.Bid = D.BidFbaPlan")
                .addField("B.bid sourceBid,B.Vgsbm vgsbm,B.VgsJc vgsJc,B.ShopName shopName,B.CreationMethod creationMethod,B.Vgsbm vgsbm,B.VgsJc vgsjc")
                .addLeftJoin("YS_ProcInst P ON P.Bid = B.Bid ")
                .addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
                .addLeftJoin("TCC_FbaLogisticsMethod C ON C.SourceBid = B.Bid AND C.SourceType = 0")
                .addField("C.LogsticsWay logsticsWay,C.Cck cck,C.nameLogsticsClearance nameLogsticsClearance")
                .addField("C.transportationCn transportationCn,C.remark remarkWl,C.vck vck,C.clearanceOpeAmount clearanceOpeAmount,C.jfFee jfFee")
                .addField("C.pzFee pzFee,C.CzFee czFee")
                .addField("C.additionalBilling")
                .addField("case when (A.numProName - C.FreeProNameNum) < 0 then 0 else A.numProName - C.FreeProNameNum end as numProNameSf")

                .addField("case when C.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
                .addField("CAST('实际物流费（总）=实际计重费（￥）+实际附加费（￥）+清关服务费(￥)' AS VARCHAR(255))  priceTranPredictSm")
                .where("A.SourceType=0");//排序;

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }


    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TccFbaFeeDetails setSeachSql1(TccFbaFeeDetails cond){

        cond.addLeftJoin("TCC_FbaDeliver D ON D.Bid = A.SourceBid AND A.SourceType = 1")
                .addField("D.bid fbaBoxUpBid")
                .addLeftJoin("TCC_FbaPlan B ON B.Bid = D.FbaPlanBid")
                .addField("B.bid sourceBid,B.Vgsbm vgsbm,B.VgsJc vgsJc,B.ShopName shopName,B.CreationMethod creationMethod,B.Vgsbm vgsbm,B.VgsJc vgsjc")
                .addLeftJoin("YS_ProcInst P ON P.Bid = B.Bid ")
                .addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
                .addLeftJoin("TCC_FbaLogisticsMethod C ON C.SourceBid = D.Bid AND C.SourceType = 1")
                .addField("C.LogsticsWay logsticsWay,C.Cck cck,C.nameLogsticsClearance nameLogsticsClearance")
                .addField("C.transportationCn transportationCn,C.remark remarkWl,C.vck vck,C.clearanceOpeAmount clearanceOpeAmount,C.jfFee jfFee")
                .addField("C.pzFee pzFee,C.CzFee czFee")
                .addField("C.additionalBilling")
                .addField("case when (A.numProName - C.FreeProNameNum) < 0 then 0 else A.numProName - C.FreeProNameNum end as numProNameSf")
                .addField("case when C.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
                .addField("CAST('实际物流费（总）=实际计重费（￥）+实际附加费（￥）+清关服务费(￥)' AS VARCHAR(255))  priceTranPredictSm")
                .where("A.SourceType=1");//排序;

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }


}
